package com.jerry.base.io.properties;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * User: Jerry
 * Date: 2024/1/28
 * Time: 11:13
 * Description:
 */
@Slf4j
public class PropertiesUsage {

    @Test
    public void read() throws IOException {
        String filePath = "src/main/resources/io/mysql.properties";
        Properties properties = new Properties();
        properties.load(new FileReader(filePath));

        properties.list(System.out);

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        log.info("user: {}", user);
        log.info("password: {}", password);
    }

    @Test
    public void create() throws IOException {
        String filePath = "src/main/resources/io/create.properties";

        Properties properties = new Properties();
        // 中文保存的是Unicode编码
        properties.setProperty("name", "宝宝");
        properties.setProperty("age", "18");

        properties.store(new FileWriter(filePath), "我是注释");
    }

    @Test
    public void update() throws IOException {
        String filePath = "src/main/resources/io/create.properties";
        Properties properties = new Properties();
        properties.load(new FileReader(filePath));

        properties.setProperty("age", "35");
        properties.store(new FileWriter(filePath), "我是产长了好多好多好多的注释");
    }
}
